const vscode = require('vscode');
const path = require('path');
const { getSettingConfig } = require('./utils');

class IssuesTreeViewProvider {
    constructor() {
        this._onDidChangeTreeData = new vscode.EventEmitter();
        this.onDidChangeTreeData = this._onDidChangeTreeData.event;
        // 根据需要设置树结构的数据
        this.data = [];
        this.iconPathMap = {
            BUG: path.join(__filename, '../../resource/error.svg'),
            VULNERABILITY: path.join(__filename, '../../resource/error.svg'),
            CODE_SMELL: path.join(__filename, '../../resource/warn.svg'),
        };
    }

    getIconPath(i) {
        return this.iconPathMap[i.type] || this.iconPathMap.CODE_SMELL;
    }

    getTreeItem(element) {
        return element;
    }

    getChildren(element) {
        const settingsConfig = getSettingConfig();

        if (!element) {
            // 返回根节点
            return this.data.map(item => {

                const result = {
                    ...item,
                    ...new vscode.TreeItem(item.label, vscode.TreeItemCollapsibleState.Collapsed),
                    description: settingsConfig.projectkey
                };

                if (item.type) {
                    result.iconPath = {
                        dark: this.getIconPath(item),
                        light: this.getIconPath(item)
                    }
                }

                return result;
            });
        } else {
            // 返回子节点
            return element.children.map(item => {
                const result = {
                    ...item,
                    ...new vscode.TreeItem(item.label, item.children ? vscode.TreeItemCollapsibleState.Collapsed : vscode.TreeItemCollapsibleState.None),
                    description: settingsConfig.projectkey
                };

                if (item.type) {
                    result.iconPath = {
                        dark: this.getIconPath(item),
                        light: this.getIconPath(item)
                    }
                }

                return result;
            });
        }
    }

    refresh(data) {
        this.data = data;

        this._onDidChangeTreeData.fire();
    }
}

class HotspotsTreeViewProvider {
    constructor() {
        this._onDidChangeTreeData = new vscode.EventEmitter();
        this.onDidChangeTreeData = this._onDidChangeTreeData.event;
        // 根据需要设置树结构的数据
        this.data = [];
        this.iconPathMap = {
            HIGH: path.join(__filename, '../../resource/error.svg'),
            LOW: path.join(__filename, '../../resource/warn.svg'),
        };
    }

    getIconPath(i) {
        return this.iconPathMap[i.vulnerabilityProbability] || this.iconPathMap.LOW;
    }

    getTreeItem(element) {
        return element;
    }

    getChildren(element) {
        const settingsConfig = getSettingConfig();

        if (!element) {
            // 返回根节点
            return this.data.map(item => {

                const result = {
                    ...item,
                    ...new vscode.TreeItem(item.label, vscode.TreeItemCollapsibleState.Collapsed),
                    description: settingsConfig.projectkey
                };

                if (item.status) {
                    result.iconPath = {
                        dark: this.getIconPath(item),
                        light: this.getIconPath(item)
                    }
                }

                return result;
            });
        } else {
            // 返回子节点
            return element.children.map(item => {
                const result = {
                    ...item,
                    ...new vscode.TreeItem(item.label, item.children ? vscode.TreeItemCollapsibleState.Collapsed : vscode.TreeItemCollapsibleState.None),
                    description: settingsConfig.projectkey
                };

                if (item.status) {
                    result.iconPath = {
                        dark: this.getIconPath(item),
                        light: this.getIconPath(item)
                    }
                }

                return result;
            });
        }
    }

    refresh(data) {
        this.data = data;

        this._onDidChangeTreeData.fire();
    }
}

module.exports = {
    IssuesTreeViewProvider,
    HotspotsTreeViewProvider
};